/*
 * Copyright 2004-2005 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.templet;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.easymock.EasyMock;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.Assert;

import com.companyname.common.base.dao.DAO;
import com.companyname.common.base.manager.Manager;



/**
 * 单元测试用例模板
 *
 * @author amonlei
 *
 */

public abstract class AbstractTestCaseTemplet
{
        static Logger logger = Logger.getLogger(AbstractTestCaseTemplet.class);
        
        private ApplicationContext applicationContext;
        
        public ApplicationContext getApplicationContext()
        {
                return this.applicationContext;
        }
        
        public void setApplicationContext(ApplicationContext applicationContext)
        {
                this.applicationContext = applicationContext;
        }
        
        //  加载spring配置文件
        public ApplicationContext getApplicationContextFromIocXmls(String[] files)
        {
                logger.debug("加载spring配置文件");
                long s = System.currentTimeMillis();
                ApplicationContext applicationContext = new ClassPathXmlApplicationContext(files);
                logger.debug("加载完毕  " + (System.currentTimeMillis() - s)/1000 + " s");
                return applicationContext;
        }
        
        public void initApplicationContext()
        {
                if(this.getApplicationContext() == null)
                {
                        this.applicationContext = this.getMockedApplicationContext();
                }
        }
        
        // 放入空的application里面
        public void putBean2EmptyMockApplicationContext(String beanId,Object beanObj)
        {
                this.putBeans2EmptyMockApplicationContext(new String[] {beanId},
                                                          new Object[] {beanObj});
        }
        
        // 放入空的application里面
        public void putBeans2EmptyMockApplicationContext(String[] beanIds,Object[] beanObjs)
        {
                this.initApplicationContext();
                EasyMock.reset(this.getApplicationContext());
                Assert.assertEquals(beanObjs.length,beanObjs.length);
                for(int i = 0; i < beanIds.length; i ++)
                {
                        this.getApplicationContext().getBean(beanIds[i]);
                        EasyMock.expectLastCall().andReturn(beanObjs[i]).anyTimes();
                }
                EasyMock.replay(this.getApplicationContext());
        }
        
        public Object getBeanFromSpring(String beanIdOrName)
        {
                return this.getApplicationContext().getBean(beanIdOrName);
        }
        
        public Timestamp now()
        {
                return new Timestamp(System.currentTimeMillis());
        }
        
        // mock applicationContext 对象        
        public ApplicationContext getMockedApplicationContext()
        {
                return (ApplicationContext)EasyMock.createMock(ApplicationContext.class);
        }
        
        // mock request 对象        
        public HttpServletRequest getMockedHttpServletRequest()
        {
                return (HttpServletRequest)EasyMock.createMock(HttpServletRequest.class);
        }
        
        // mock session 对象
        public HttpSession getMocekdHttpSession()
        {
                return (HttpSession)EasyMock.createMock(HttpSession.class);
        }
        
        // mock dao对象        
        public DAO getMockedDAO(Class cl)
        {
                Assert.assertNotNull(cl);
                DAO dao = (DAO)EasyMock.createMock(cl);
                return dao; 
        }
        
        // mock manager对象        
        public Manager getMockedManager(Class cl)
        {
                Assert.assertNotNull(cl);
                Manager manager = (Manager)EasyMock.createMock(cl);
                return manager; 
        }
        
}
